/**
 * 该文件的主要用于：
 * 1.定义：actions,mutationd,state
 * 2.创建一个Vuex中最核心的部分---store
 * 3.且store要管理：actions, mutations,state
*/
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import { nanoid } from 'nanoid'
Vue.use(Vuex)

//定义一个actions 
const actions ={
     // 一言网址 https://hitokoto.cn/
     //段子 https://api.apiopen.top/getJoke?type=text
    addSaying(miniStore){
        axios.get('http://api.uixsj.cn/hitokoto/get?type=social').then(
          
        response =>{
            miniStore.commit('ADD_SAYING',response.data)
            },error=>{
                alert(error)
            }
        )
    }
}
//定义一个mutations
const mutations = {
	INCREMENT_SCHOOL_AGE(state,value){
        state.school.age += value;
    },
    ADD_SAYING(state,value){
        const sayingObj= {id:nanoid(),title:value}
        state.sayingList.unshift(sayingObj)
    }
}
//定义一个state 
const state = {
school:{
    name:'尚硅谷',
    address:'宏福科技园',
    tel:'010-56253825',
    age:8
},
sayingList:[
    {id:'123455',title:'寒月潇潇月满轩，风过云清雾影前'}
]
   
}

 const getters = {
    
 }
//创建并暴露一个store 并且管理：actions,mutations,state
export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})